﻿
using System;

namespace GECS.Input {

	/// <summary>
	/// A class representing an input action.
	/// </summary>
    public class Action {

        GInput.Behaviour DETECTION_TYPE = GInput.Behaviour.NORMAL;
        State     STATE          = State.RELEASED;
        int       AMOUNT         = 0;

        /// <summary>
        /// Gets or sets the detection type.
        /// </summary>
        public GInput.Behaviour DetectionType {
            get {
                return DETECTION_TYPE;
            } set {
                DETECTION_TYPE = value;
            }
        }

        /// <summary>
        /// Resets this action.
        /// </summary>
        public void Reset() {
            AMOUNT = 0;
            STATE = State.RELEASED;
        }

        /// <summary>
        /// Presses this action once.
        /// </summary>
        public void Press() {
            Press(1);
        }

        /// <summary>
        /// Presses this action with a certain amount.
        /// </summary>
        /// <param name="amount">The no of times pressed.</param>
        public void Press(int amount) {
            if (STATE != State.WAITING_FOR_RELEASE) {
                AMOUNT += amount;
                STATE = State.PRESSED;
            }
        }

        /// <summary>
        /// Releases this action.
        /// </summary>
        public void Release() {
            STATE = State.RELEASED;
        }

        /// <summary>
        /// Taps this action. Same as calling Press() followed by Release().
        /// </summary>
        public void Tap() {
            Press();
            Release();
        }

        /// <summary>
        /// Gets you the amount of this action.
        /// </summary>
        public int Amount {
            get {
                int retVal = AMOUNT;
                if (STATE == State.RELEASED) {
                    AMOUNT = 0;
                } else if (DETECTION_TYPE == GInput.Behaviour.INITIAL_DETECT) {
                    STATE = State.WAITING_FOR_RELEASE;
                    AMOUNT = 0;
                }
                return retVal;
            }
        }

        /// <summary>
        /// Check if the action is pressed.
        /// </summary>
        /// <returns>True if pressed. Else False.</returns>
        public bool IsPressed() {
            return (Amount != 0);
        }

    }

}
